package br.com.rapidrest.demo.web.phonebook;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;

import br.com.rapidrest.annotation.Inject;
import br.com.rapidrest.demo.domain.PhoneBook;
import br.com.rapidrest.demo.domain.PhoneEntry;
import br.com.rapidrest.demo.service.PhoneBookService;
import br.com.rapidrest.demo.web.Home;
import br.com.rapidrest.response.RestURL;
import br.com.rapidrest.util.MessagesBundle;

public class EditPhoneBook implements Serializable {

	private static final long serialVersionUID = -2502601893113156087L;

	@Inject
	private MessagesBundle messages;

	@EJB
	private PhoneBookService phoneBookService;

	private PhoneBook phoneBook;

	private String name;

	private String entryName;

	private String entryPhone;

	public Object init(Long phoneBookId) {
		phoneBook = phoneBookService.findPhoneBook(phoneBookId);
		if (phoneBook == null) {
			return Home.class;
		}
		return null;
	}

	public Object save(Long phoneBookId) {
		PhoneBook phoneBook = phoneBookService.findPhoneBook(phoneBookId);
		if (phoneBook == null) {
			return Home.class;
		}
		phoneBook.setName(name);
		phoneBookService.updatePhoneBook(phoneBook);
		return Home.class;
	}

	public Object addEntry(Long phoneBookId) {
		PhoneBook phoneBook = phoneBookService.findPhoneBook(phoneBookId);
		if (phoneBook == null) {
			return Home.class;
		}
		PhoneEntry entry = new PhoneEntry();
		entry.setName(entryName);
		entry.setPhone(entryPhone);
		phoneBook.addEntry(entry);
		phoneBookService.updatePhoneBook(phoneBook);
		return RestURL.goTo(EditPhoneBook.class, phoneBookId);
	}
	
	public Object removeEntry(Long phoneBookId, Long entryId) {
		PhoneBook phoneBook = phoneBookService.findPhoneBook(phoneBookId);
		Iterator<PhoneEntry> entries = phoneBook.getEntries().iterator();
		while (entries.hasNext()) {
			PhoneEntry entry = entries.next();
			if (entry.getId().equals(entryId)) {
				entries.remove();
				break;
			}
		}
		phoneBookService.updatePhoneBook(phoneBook);
		return RestURL.goTo(EditPhoneBook.class, phoneBookId);
	}
	
	public List<PhoneEntry> getEntries() {
		List<PhoneEntry> entries = phoneBook.getEntries();
		Collections.sort(entries, new Comparator<PhoneEntry>() {

			@Override
			public int compare(PhoneEntry e1, PhoneEntry e2) {
				final Comparator<String> strComparator = String.CASE_INSENSITIVE_ORDER;
				int compare = strComparator.compare(e1.getName(), e2.getName());
				
				if (compare != 0) {
					return compare;
				}
				
				return strComparator.compare(e1.getPhone(), e2.getPhone());
			}

		});
		return entries;
	}

	public MessagesBundle getMessages() {
		return messages;
	}

	public PhoneBook getPhoneBook() {
		return phoneBook;
	}

	public void setPhoneBook(PhoneBook phoneBook) {
		this.phoneBook = phoneBook;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEntryName(String entryName) {
		this.entryName = entryName;
	}

	public void setEntryPhone(String entryPhone) {
		this.entryPhone = entryPhone;
	}

}